﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Framework.FSM;


namespace BridgeShips
{
    public class BridgeShipAniController :MonoBehaviour
    {
        public static BridgeShipAniController Instance;
        private GameObject[] m_CarObj;
        private List<ObjectOriginalMessage> m_partObj = new List<ObjectOriginalMessage>();
        public FsmStateDataBase m_fsmdb;
        private bool m_isStartAnimation = false;
        private void Awake()
        {
            Instance = this;
        }
        void Start()
        {
            m_CarObj = new GameObject[transform.childCount];
            for (int i = 0; i < transform.childCount; i++)
            {
                m_CarObj[i] = transform.GetChild(i).gameObject;
                var m_partMsgs = m_CarObj[i].GetComponentsInChildren<ObjectOriginalMessage>();
                foreach (var item in m_partMsgs)
                {
                    item.OnInit();
                    //item.m_ParentObj = m_CarObj[i];
                    m_partObj.Add(item);
                }
            }
            m_fsmdb = new FsmStateDataBase();
            m_fsmdb.m_CurrentIndex = 0;
            m_fsmdb.m_Obj = this.gameObject;
            InitCarPos();

        }
        private void Update()
        {
            if (Input.GetKeyDown(KeyCode.V))
            {
                //GameEntry.Instance.FsmManager.GetFsm("BSAnimation").ChangeState<BridgeExpansion>(m_fsmdb);
                StartAnimation();
            }
            if (Input.GetKeyDown(KeyCode.B))
            {
                ResetAnimation();
            }
        }
        private void InitCarPos() {

           // GameEntry.Instance.FsmManager.CreateFsm(this.gameObject, "BSAnimation", GetCarAnimState());

        }
        private List<IFsmState> GetCarAnimState() {
            List<IFsmState> listFSM = new List<IFsmState>();
            listFSM.Add(new BridgeExpansion());
            listFSM.Add(new CarMove());
            listFSM.Add(new CarAnimation());
            listFSM.Add(new ShipMoveInWater());
            listFSM.Add(new CarAnimationBack());

            return listFSM;
        }
        /// <summary>
        /// 开始播放动画
        /// </summary>
        public void StartAnimation() {
            if (m_isStartAnimation)
            {
                return;
            }
            m_isStartAnimation = true;
            GameEntry.Instance.FsmManager.GetFsm("BSAnimation").ChangeState<BridgeExpansion>(m_fsmdb);
        }
        /// <summary>
        /// 重置动画
        /// </summary>
        public void ResetAnimation() {
            if (!m_isStartAnimation)
            {
                return;
            }
            m_isStartAnimation = false;
            GameEntry.Instance.FsmManager.GetFsm("BSAnimation").ResetFsm();//.ChangeState<BridgeExpansion>(m_fsmdb);
            //所有部件恢复原位
            foreach (var item in m_partObj)
            {
                item.transform.SetParent(item.m_ParentObj.transform);
                item.transform.localPosition = item.m_OriginalPos;
                item.transform.localRotation = item.m_OriginalQua;
                item.transform.localScale = item.m_OriginalSca;
             
            }
            //将动画脚本中所有的条件置为false
            //TODO 
            for (int i = 0; i < transform.childCount; i++)
            {
                transform.GetChild(i).GetComponent<transportCarAnimation>().Reset();
            }
          
        }
    }

}
